package com.sinosoft.perfect.system._.dao;

import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.common.system.BaseSqlSessionDaoSupport;
import com.sinosoft.perfect.system._.MapperSystem;
import com.sinosoft.perfect.system._.po.TLoginLog;
import com.sinosoft.perfect.system._.po.TRoleSystem;
import com.sinosoft.perfect.system._.po.TUnit;
import com.sinosoft.perfect.system._.po.TUser;
import com.sinosoft.perfect.system._.po.TUserImg;
import com.sinosoft.perfect.system._.util.Producer;
import com.sinosoft.perfect.system._.vo.LoginUser;
import com.sinosoft.perfect.system._.vo.LoginWroksiteUnitValidate;

public class LoginDaoImpl extends BaseSqlSessionDaoSupport implements ILoginDao {

	private MapperSystem getMapperSystem(){
		return getSqlSession().getMapper(MapperSystem.class);
	}
	
	public String validateUsernamePassword(TUser tuserInfo) throws SQLException{
		return "" + getMapperSystem().validateLogin(tuserInfo);
	}
	
	public List<LoginWroksiteUnitValidate> queryValidateLoginWorkSiteIp(String userId) throws SQLException{
		return getMapperSystem().queryValidateLoginWorkSiteIp(userId);
	}
	
	
	public LoginUser queryUserInfo(String username) throws SQLException{
		Object obj = getMapperSystem().selectLoginUserInfoByUsername(username);
		return obj==null?null:(LoginUser)obj;
	}
	
	public TLoginLog insertUserLoginLog(String loginIpAddress,String userMainId) throws SQLException{
		TLoginLog tloginLog = getTLoginLogClassForInsertLogInfo(userMainId,loginIpAddress,"IN","");
		getMapperSystem().insertLogByTLoginLog(tloginLog);
		return tloginLog;
	}

	private TLoginLog getTLoginLogClassForInsertLogInfo(String userId,String loginIp,String inOut,String comments){
		TLoginLog tloginLog = new TLoginLog();
		String logId = Producer.generateIdOfTen();
		tloginLog.setLogId(logId);
		tloginLog.setUserMainId(userId);
		tloginLog.setLoginIp(loginIp);
		tloginLog.setIsInOut(inOut);
		tloginLog.setComments(comments);
		return tloginLog;
	}

	@Override
	public List<TUnit> queryUserUnitsInfo(String userId) throws SQLException {
		return getMapperSystem().selectLoginUserInfoOfUnitsByUserId(userId);
	}
	
	@Override
	public List<TRoleSystem> queryUserRoleSystemsInfo(String userId)
			throws SQLException {
		return getMapperSystem().selectLoginUserInfoOfRoleSystemsByUserId(userId);
	}

	@Override
	public void logoutUpdateLogInfo(String logId) throws SQLException {
		getMapperSystem().updateLogoutByPrimaryKey(logId);
	}

	@Override
	public void registerLoginStatus(String loginId) throws SQLException {
		getMapperSystem().registerLoginStatus(loginId);
	}

	@Override
	public TUserImg selectLoginImgInfo(String userMainId) throws SQLException {
		return getMapperSystem().selectLoginImgInfo(userMainId);
	}
}
